package com.toocms.jinjuhang.ui.mine.management_system.institution_address

import android.app.Application
import android.os.Bundle
import android.text.TextUtils
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.center.AddressDetailsData
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.Messenger
import com.toocms.tab.bus.event.SingleLiveEvent
import com.toocms.tab.map.TabMapApi

class InstitutionAddressModel(application: Application, val bundle: Bundle?) :
    BaseViewModel<BaseModel>(application) {

    val organizationId by lazy {
        bundle?.getString(Constants.BundleKey.KEY_ORGANIZATION_ID) ?: ""
    }

    val dataObservableField by lazy {
        val content = bundle?.getParcelable(Constants.BundleKey.KEY_ADDRESS) ?: AddressDetailsData()
        ObservableField(content)
    }


    val addressChangeSingleLiveEvent = SingleLiveEvent<String>()

    val locationClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        TabMapApi.getChoosingApi().startPreciseLocationPageForResult(this) { poiItem ->
            dataObservableField.get()?.apply {
                this.province_name = poiItem.province
                this.city_name = poiItem.city
                this.district_name = poiItem.district
                this.district_id = poiItem.districtCode
                this.latitude = poiItem.latitude
                this.longitude = poiItem.longitude
                dataObservableField.notifyChange()
                addressChangeSingleLiveEvent.value = poiItem.name
            }
        }
    })

    val saveClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        dataObservableField.get()?.apply {
            if (TextUtils.isEmpty(district_id)) {
                showToast(R.string.str_no_selected_address_hint)
                return@BindingAction
            }
            val fullAddress = "$province_name $city_name $district_name $address"
//            SaveReceiverAddressDialog().apply {
//                this@apply.address = fullAddress
//                onPositiveClickListener = {
//
//                }
//                show((ActivityUtils.getTopActivity() as BaseActivity).supportFragmentManager, null)
//        }
            val params = mutableMapOf<String, String?>()
            params["member_id"] = UserRepository.user.member_id
            params["organization_id"] = organizationId
            params["contacts"] = contacts
            params["mobile"] = mobile
            params["latitude"] = latitude
            params["longitude"] = longitude
            params["province_name"] = province_name
            params["city_name"] = city_name
            params["district_name"] = district_name
            params["district_id"] = district_id
            params["address"] = address
            requestData(
                Urls.Center.SET_ORGANIZATION_ADDRESS,
                params,
                resultClass = String::class.java,
                result = {
                    showToast(it)
                    Messenger.getDefault()
                        .sendNoMsg(Constants.MessengerToken.TOKEN_REFRESH_BALANCE)
                    this@InstitutionAddressModel.finishFragment()
                })
        }
    })

}